package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.ApplicationRequirementFactoryVO;
import com.sc.nft.enums.ApplicationRequirementEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ApplicationRequirementFactoryService;
import com.sc.nft.util.DecideUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
@Service
public class ImmediateAccountServiceImpl implements ApplicationRequirementFactoryService {
    private final UserRelevanceDao userRelevanceDao;
    private final UserMarketValueDao userMarketValueDao;

    /**
     * 申请条件: 直属账户(直推交易用户)
     *
     * @param userInfo
     * @param applicationRequirement
     * @param type
     * @return
     */
    @Override
    public ApplicationRequirementFactoryVO applicationRequirement(UserInfo userInfo, ApplicationRequirement applicationRequirement, Integer type) {
        List<UserRelevance> directThrustCount = userRelevanceDao.directThrustCount(userInfo.getId());
        // 通过directThrustCount中的relevanceId字段来获取表sc_user_market_value表中的userId的数据,判定yesterday_market_value字段是否大于1000
        List<Long> userIds = directThrustCount.stream().map(UserRelevance::getRelevanceId).collect(Collectors.toList());
        String value = applicationRequirement.getValue();
        List<String> split = StrUtil.split(value, ",");
        Integer userIdsPositionCount=null;
        if (CollUtil.isEmpty(userIds)) {
            userIdsPositionCount=0;
        }else {
            userIdsPositionCount = userMarketValueDao.getUserIdsPositionCount(userIds, Convert.toBigDecimal(split.get(0)));
        }
        ApplicationRequirementFactoryVO applicationRequirementFactoryVO = new ApplicationRequirementFactoryVO();
        applicationRequirementFactoryVO.setKey(StrUtil.format("{}{}{}", applicationRequirement.getType().getDescription(), applicationRequirement.getJudgingCriteria(), Convert.toInt(split.get(1))));
        applicationRequirementFactoryVO.setSmallCharacter(StrUtil.format("{}{}",applicationRequirement.getType().getDescriptionMinCharacters(),Convert.toInt(split.get(0))));        applicationRequirementFactoryVO.setAskForValue(split.get(1));
        applicationRequirementFactoryVO.setApplicationRequirementEnum(ApplicationRequirementEnum.IMMEDIATE_ACCOUNT);
        applicationRequirementFactoryVO.setPracticalValue(userIdsPositionCount);
        applicationRequirementFactoryVO.setPracticalValueStr(userIdsPositionCount);
        // 根据compoundMode字段中的 > < >= <= = 五中 来判定是否满足条件
        applicationRequirementFactoryVO.setSatisfy(DecideUtil.conditionalDecision(userIdsPositionCount, Convert.toInt(split.get(1)), applicationRequirement.getJudgingCriteria()));
        return applicationRequirementFactoryVO;
    }
    @Override
    public Integer code() {
        return ApplicationRequirementEnum.IMMEDIATE_ACCOUNT.getCode();
    }
}
